package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"take-out-master/cache"
	"take-out-master/models"
	"take-out-master/pkg"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	redis_login_key := "redis_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_login_key).Int()
	if errCount == 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.MD5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_login_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_login_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次，封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
func RoleNote(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		 *
		FROM
		  notes
		  LEFT JOIN role_notes ON role_notes.note_id = notes.id
		WHERE
		  role_notes.note_id = 1
	*/
	var role models.Note

	models.DB.Table("notes").
		Joins("LEFT JOIN role_notes ON role_notes.note_id = notes.id").
		Where("role_notes.role_id = ?", rid).
		Debug().
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func UserRole(c *gin.Context) {
	uid := c.Query("uid")

	/*
		SELECT * FROM
		roles
		LEFT JOIN user_roles on user_roles.role_id = roles.id
	*/
	var user models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles on user_roles.role_id = roles.id").
		Where("user_roles.role_id = ?", uid).
		Debug().
		Find(&user)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return
}
func PeiAdd(c *gin.Context) {
	PeiName := c.PostForm("PeiName")
	Stat := c.PostForm("Stat")
	pei := models.Pei{

		PeiName: PeiName,
		Stat:    Stat,
	}
	err := models.DB.Create(&pei).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Order(c *gin.Context) {
	DingName := c.PostForm("DingName")
	PeiID, _ := strconv.Atoi(c.PostForm("PeiID"))
	var ding = models.Ding{

		DingName: DingName,
		PeiID:    PeiID,
	}
	err := models.DB.Create(&ding).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var list []models.Pout
	redis_list_key := "redis_list_" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()
	/*
		SELECT
		  dings.id as ID,
		  peis.pei_name as PeiName,
		  peis.stat as Stat,
		  dings.ding_name as DingName,
		  dings.pei_id as PeiID
		FROM
		  dings
		  LEFT JOIN peis ON dings.pei_id = peis.id
		WHERE
		  dings.id = 1
	*/

	if result == "" {
		models.DB.Table("dings").
			Select(
				"dings.id as ID",
				" peis.pei_name as PeiName",
				"peis.stat as Stat",
				" dings.ding_name as DingName",
				"dings.pei_id as PeiID",
			).
			Joins("LEFT JOIN peis ON dings.pei_id = peis.id").
			Debug().
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(result), &list)
	}
	var total int64
	models.DB.Model(&models.Ding{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  list,
		"total": total,
	})
	return
}
